<?php


namespace App\Api;
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,PATCH');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

use App\Domain\UserAddress as UserAddressDomain;
use PhalApi\Api;

class UserAddress extends Api
{
    public function getRules()
    {
        return array(
            'getUserAddressByUid' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'min' => 1, 'desc' => '用户的id')
            ),
            'insertUserAddressInfo' => array(
                'userAddress_name' => array('name' => 'userAddress_name', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '收件人名称'),
                'userAddress_phone' => array('name' => 'userAddress_phone', 'require' => true, 'min' => 1, 'max' => 20, 'source' => 'post', 'desc' => '收件人电话'),
                'userAddress_detail' => array('name' => 'userAddress_detail', 'require' => true, 'min' => 1, 'max' => 150, 'source' => 'post', 'desc' => '收件人详细地址'),
                'userAddress_uid' => array('name' => 'userAddress_uid', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '收货地址所属用户的id'),
            ),
            'getUserAddressByUaId' => array(
                'ua_id' => array('name' => 'ua_id', 'require' => true, 'min' => 1, 'desc' => '用户收货地址的id')
            ),
        );
    }

    /**
     * 根据用户id查看该用户的所有地址
     * @desc 获取指定用户的所有地址
     */
    public function getUserAddressByUid()
    {
        $user_id = $_GET['user_id'];
        $domain = new UserAddressDomain();
        $info = $domain->getUserAddressByUid($user_id);
        return array('info' => $info);
    }

    /**
     * 增加新的用户收获地址信息
     * @desc 增加新的用户收获地址信息，通过收件人，电话，详细住址，以及用户的id这四个参数实现
     * 在所登录的用户中新增收货地址信息
     */
    public function insertUserAddressInfo()
    {
        $userAddress_name = $_POST['userAddress_name'];
        $userAddress_phone = $_POST['userAddress_phone'];
        $userAddress_detail = $_POST['userAddress_detail'];
        $userAddress_uid = $_POST['userAddress_uid'];
        $domain = new UserAddressDomain();
        $id = $domain->insertUserAddressInfo($userAddress_name, $userAddress_phone, $userAddress_detail, $userAddress_uid);
        return array('id' => $id);
    }

    public function getUserAddressByUaId()
    {
        $ua_id = $_GET['ua_id'];
        $domain = new UserAddressDomain();
        $info = $domain->getUserAddressByUaId($ua_id);
        return array('info' => $info);
    }
}